package com.hy.appium1;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class AppiumDemo02Element {

//    AppiumDriver driver;


    AndroidDriver driver;

    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.handsgo.jiakao.android");
        //默认启动不初始化软件
//        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.handsgo.jiakao.android.splash.Login");
        //创建驱动
        driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
//        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() throws InterruptedException {

//元素点击操作
        WebElement byId = driver.findElementById("com.handsgo.jiakao.android:id/btn_agree");
        byId.click();

        //不再提醒   com.handsgo.jiakao.android:id/permission_box


        driver.findElementById("com.handsgo.jiakao.android:id/permission_box").click();

        //关闭         com.handsgo.jiakao.android:id/permission_close


        //知道了       com.handsgo.jiakao.android:id/permission_btn
        driver.findElementById("com.handsgo.jiakao.android:id/permission_btn").click();

        //  定位中   com.handsgo.jiakao.android:id/cityTv

        driver.findElementById("com.handsgo.jiakao.android:id/cityTv").click();

        //文本框的id    com.handsgo.jiakao.android:id/edt_search_q
        WebElement webElement = driver.findElementById("com.handsgo.jiakao.android:id/edt_search_q");

        webElement.sendKeys("上");

        //搜索到的具体的位置  com.handsgo.jiakao.android:id/item_title


        List<WebElement> list = driver.findElementsById("com.handsgo.jiakao.android:id/item_title");

        for (WebElement element : list) {
            System.out.println(element.getText());

        }

        list.get(0).click();


        //  男     com.handsgo.jiakao.android:id/maleTv

        driver.findElementById("com.handsgo.jiakao.android:id/maleTv").click();

        // 已报名驾校     com.handsgo.jiakao.android:id/unRegisterTv

        driver.findElementById("com.handsgo.jiakao.android:id/registeredTv").click();

        //下一步   com.handsgo.jiakao.android:id/okBtn

        driver.findElementById("com.handsgo.jiakao.android:id/okBtn").click();

        //点击驾校名     com.handsgo.jiakao.android:id/hot1

        driver.findElementById("com.handsgo.jiakao.android:id/hot1").click();

        //选价钱    com.handsgo.jiakao.android:id/stage_2000_4000

        driver.findElementById("com.handsgo.jiakao.android:id/stage_2000_4000").click();

        //点击弹窗的关闭

        driver.findElementById("com.handsgo.jiakao.android:id/closeIv").click();

        String ex = ".main.activity.MainActivity";

        String currentActivity = driver.currentActivity();

        System.out.println(currentActivity);

        Assert.assertEquals(currentActivity,ex);


    }

    @AfterMethod
    public void TestDown() {
//        driver.quit();
    }
}
